package com.ruoyi.project.dict.materialTopic.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.materialTopic.domain.MaterialTopic;
import com.ruoyi.project.dict.materialTopic.service.IMaterialTopicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资料专题Controller
 *
 * @author shixiangwen
 * @date 2022-02-24
 */
@Controller
@RequestMapping("/dict/materialTopic")
public class MaterialTopicController extends BaseController {

    private String prefix = "dict/materialTopic";

    @Autowired
    private IMaterialTopicService materialTopicService;

    @RequiresPermissions("dict:materialTopic:view")
    @GetMapping()
    public String materialTopic() {
        return prefix + "/materialTopic";
    }

    /**
     * 查询资料专题列表
     */
    @RequiresPermissions("dict:materialTopic:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MaterialTopic materialTopic) {
        startPage();
        List<MaterialTopic> list = materialTopicService.selectMaterialTopicList(materialTopic);
        return getDataTable(list);
    }

    /**
     * 导出资料专题列表
     */
    @RequiresPermissions("dict:materialTopic:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MaterialTopic materialTopic) {
        List<MaterialTopic> list = materialTopicService.selectMaterialTopicList(materialTopic);
        ExcelUtil<MaterialTopic> util = new ExcelUtil<MaterialTopic>(MaterialTopic.class);
        return util.exportExcel(list, "materialTopic");
    }

    /**
     * 新增资料专题
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存资料专题
     */
    @RequiresPermissions("dict:materialTopic:add")
    @Log(title = "资料专题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MaterialTopic materialTopic) {
        return toAjax(materialTopicService.insertMaterialTopic(materialTopic));
    }

    /**
     * 修改资料专题
     */
    @GetMapping("/edit/{topicId}")
    public String edit(@PathVariable("topicId") Long topicId, ModelMap mmap) {
        MaterialTopic materialTopic = materialTopicService.selectMaterialTopicById(topicId);
        mmap.put("materialTopic", materialTopic);
        return prefix + "/edit";
    }

    /**
     * 修改保存资料专题
     */
    @RequiresPermissions("dict:materialTopic:edit")
    @Log(title = "资料专题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MaterialTopic materialTopic) {
        return toAjax(materialTopicService.updateMaterialTopic(materialTopic));
    }

    /**
     * 删除资料专题
     */
    @RequiresPermissions("dict:materialTopic:remove")
    @Log(title = "资料专题", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(materialTopicService.deleteMaterialTopicByIds(ids));
    }

    @GetMapping("/search")
    @ResponseBody
    public TableDataInfo search(String q) {
        startPage();
        MaterialTopic materialTopic = new MaterialTopic();
        materialTopic.setName(q);
        List<MaterialTopic> list = materialTopicService.selectMaterialTopicList(materialTopic);
        return getDataTable(list);
    }
}
